/**
 * Copyright(c): 2017 com.mjduan All rights reserved.
 * 项目名：learn-spring-web
 * 注意：未经作者允许，不得外传
 */
package com.mjduan.project.util.redis.subscribe;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @author mjduan@yahoo.com mjduan 2018-06-24 14:51
 * @version 1.0
 * @since 1.0
 */
@Configuration
public class SubscribeConfiguration {

    @Bean(value = "MessageListener")
    public MessageListenerAdapter getMessageListenerAdapter() {
        return new MessageListenerAdapter(new CustomMessageListener());
    }

    @Bean(value = "RedisMessageListenerContainer")
    public RedisMessageListenerContainer getRedisMessageListenerContainer(
            @Qualifier(value = "JedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory,
            @Qualifier(value = "MessageListener") MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(jedisConnectionFactory);
        List<ChannelTopic> topics = Arrays.asList(new ChannelTopic(PublishSubscribeTest.CHAT_ROOM),
                new ChannelTopic(PublishSubscribeTest.NEWS_CHANNEL));
        container.addMessageListener(messageListenerAdapter, topics);
        return container;
    }
}
